package com.wfh.sql.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wfh.sql.common.BaseResponse;
import com.wfh.sql.common.ErrorCode;
import com.wfh.sql.common.ResultUtils;
import com.wfh.sql.constant.UserConstant;
import com.wfh.sql.exception.BusinessException;
import com.wfh.sql.model.dto.UserLoginRequest;
import com.wfh.sql.model.dto.UserRegisterRequest;
import com.wfh.sql.model.entity.User;
import com.wfh.sql.model.vo.UserVo;
import com.wfh.sql.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Title: UserControler
 * @Author wangfenghuan
 * @Package com.wfh.sql.controller
 * @Date 2025/10/17 16:03
 * @description: 用户控制器
 */
@RestController
@RequestMapping("/user")
public class UserControler {

    @Resource
    private UserService userService;


    /**
     * 用户登录
     *
     * @param userLoginRequest
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<UserVo> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        // 参数校验
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword) || userPassword.length() < 8) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 查询数据库中是否存在
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery(User.class)
                .eq(User::getUserAccount, userAccount);
        List<User> users = userService.getBaseMapper().selectList(queryWrapper);
        if (users.size() != 1) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "账号不存在或账号异常");
        }
        User user = users.get(0);
        String cryptoPwd = user.getUserPassword();
        boolean b = DigestUtil.bcryptCheck(userPassword, cryptoPwd);
        if (!b) throw new BusinessException(ErrorCode.OPERATION_ERROR, "密码错误");
        // 保存登录信息
        request.getSession().setAttribute(UserConstant.USER_LOGIN_STATE, user);
        return ResultUtils.success(BeanUtil.copyProperties(user, UserVo.class));
    }

    /**
     * 用户注册
     * @param registerRequest
     * @return
     */
    @PostMapping("/register")
    public BaseResponse<Boolean> userRegister(@RequestBody UserRegisterRequest registerRequest) {
        // 参数校验
        String userAccount = registerRequest.getUserAccount();
        String userPassword = registerRequest.getUserPassword();
        String checkPassword = registerRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword) || !userPassword.equals(checkPassword) || userPassword.length() < 8) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "两次密码输入不一致或密码长度小于8位");
        }
        List<User> users = userService.getBaseMapper().selectList(new LambdaQueryWrapper<User>().eq(User::getUserAccount, userAccount));
        if (users.size() > 1){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "账号已经存在");
        }
        String bcryptPwd = DigestUtil.bcrypt(userPassword);
        User user = new User();
        user.setUserAccount(userAccount);
        user.setUserPassword(bcryptPwd);
        boolean save = userService.save(user);
        return ResultUtils.success(save);
    }
}
